package com.aim.analysis.records;

import com.aim.analysis.records.bean.PerformanceData;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * User: Avraham Shvartzon
 * Date: 7/7/14
 */
public class PerformanceDataCollector  {

    private List<Long> collisionTimesList = new LinkedList<Long>();
    private List<Long> shutdownTimesList = new LinkedList<Long>();
    private List<Long> fireTimesList = new LinkedList<Long>();
    private int totalCollision = 0;
    private int totalShutdown = 0;
    private int totalFire =0;

    public void addPerformanceData(PerformanceData performanceData) {
        if (performanceData.collistionTimes != null) {
            for (long collisionTime : performanceData.collistionTimes) {
                collisionTimesList.add(collisionTime);
            }
        }

        if (performanceData.shutdownTimes != null) {
            for (long time : performanceData.shutdownTimes) {
                shutdownTimesList.add(time);
            }
        }

        if (performanceData.fireTimes != null) {
            for (long time : performanceData.fireTimes) {
                fireTimesList.add(time);
            }
        }

        totalCollision = Math.max(totalCollision, performanceData.totalCl);
        totalShutdown = Math.max(totalShutdown, performanceData.totalSd);
        totalFire = Math.max(totalFire, performanceData.totalFire);
    }

    public List<Long> getCollisionTimesList() {
        return collisionTimesList;
    }

    public List<Long> getShutdownTimesList() {
        return shutdownTimesList;
    }

    public List<Long> getFireTimesList() {
        return fireTimesList;
    }

    public long[] getCollisionTimesArray(){
        return Longs.toArray(getCollisionTimesList());
    }

    public long[] getShutdownTimesArray(){
        return Longs.toArray(getShutdownTimesList());
    }

    public long[] getFireTimesArray(){
        return Longs.toArray(getFireTimesList());
    }

    public int getTotalCollision() {
        return totalCollision;
    }

    public int getTotalShutdown() {
        return totalShutdown;
    }

    public int getTotalFire() {
        return totalFire;
    }

    public PerformanceData[] split(int... times){
        if (times[0] != 0){
            times = Ints.concat(new int[]{0}, times);
        }

        PerformanceData[] perfDataArray = new PerformanceData[times.length];


        for (int i = 0; i < times.length; i++) {
            int from = times[i];
            int to = i < (times.length-1) ? times[i+1] : Integer.MAX_VALUE;

            long[] clsTimes = subArrayByTimeRange(getCollisionTimesList(), from, to);
            long[] sdTimes = subArrayByTimeRange(getShutdownTimesList(), from, to);
            long[] fireTimes = subArrayByTimeRange(getFireTimesList(), from, to);

            perfDataArray[i] = new PerformanceData(clsTimes,sdTimes, fireTimes);
        }

        return perfDataArray;
    }

    private long[] subArrayByTimeRange(List<Long> src, long from, long to){
        List<Long> subList = new ArrayList<Long>();
        for (Long time : src) {
            if (from <= time && time < to){
                subList.add(time);
            }
        }
        return Longs.toArray(subList);
    }

}
